//
//  NSObject+DefalutValue.m
//  AIComponmentKitDemo
//
//  Created by 赵梁 on 2016/11/25.
//  Copyright © 2016年 zify. All rights reserved.
//

#import "NSObject+AI_DefalutValue.h"

@implementation NSObject (AI_DefalutValue)

+ (id)ai_setObject:(id)object withDefaultValue:(id)defaultValue
{
    if ([NSObject ai_isEmptyObject:object]) {
        return defaultValue;
    }
    
    if ([object isKindOfClass:[NSArray class]]
        &&[defaultValue isKindOfClass:[NSArray class]]) {
        return object;
    }else if ([object isKindOfClass:[NSDictionary class]]
              &&[defaultValue isKindOfClass:[NSDictionary class]]) {
        return object;
    }else if ([object isKindOfClass:[NSString class]]
              &&[defaultValue isKindOfClass:[NSString class]]) {
        return object;
    }else if ([object isKindOfClass:[NSNumber class]]
              &&[defaultValue isKindOfClass:[NSString class]]) {
        return [NSString stringWithFormat:@"%@",object];
    }else if (![defaultValue isKindOfClass:[object class]]) {
        return defaultValue;
    }
    
    return object;
}

+ (BOOL)ai_isEmptyObject:(id)object
{
    if ([object isEqual:[NSNull null]]) {
        return YES;
    }else if ([object isKindOfClass:[NSString class]]) {
        if ([(NSString *)object length] == 0) {
            return YES;
        }
    }else if ([object isKindOfClass:[NSArray class]]) {
        if ([(NSArray *)object count] == 0) {
            return YES;
        }
    }else if ([object isKindOfClass:[NSDictionary class]]) {
        if ([(NSDictionary *)object count] == 0) {
            return YES;
        }
    }
    return NO;
}

@end
